<?php

class Resource extends AppModel {
	
	public $name = 'Resource';
	
	public $actAs = array(
		'FileUpload.FileUpload' => array (
			'uploadDir' => 'pictures',
			'required' => true,
			'unique' => true,
			'fileNameFunction' => 'sha1'
		)
	);
	
	public function getFileName($file) {
		//pr( $file );
		$ext = pathinfo($file, PATHINFO_EXTENSION);
		$fileName = uniqid() . '.' . $ext;
		return $fileName;
	}
	
	public function beforeSave($options = array()) {
		parent::beforeSave($options);
		if( isset($this->data['Resource']['group']) ) {
			$group = $this->data['Resource']['group'];
			$name = $this->data['Resource']['name'];
			switch( $group ) {
				case 'product':
					$this->data['Resource']['path'] = WWW_ROOT . 'pictures' . DS . 'products' . DS . $name;
					$this->data['Resource']['url'] = Router::url('/pictures/products/'.$name);
					break;
			}
		}
		return true;
	}
	
}